<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    /*
           async/await 与 promise 的关系：
        1.执行async函数，返回的是promise对象
        2.await相当于promise的then
        3.try...catch可捕获异常，代替了promise的catch
    */
    /* 案例一 */
    /*  (async function () {
      const p1 = Promise.resolve(300);
      const data = await p1;
      console.log(data, "data"); //300
    })(); */
    /* 案例二 */
    /*   (async function () {
      const data1 = await 400; //await Promise.resolve(400)
      console.log(data1, "data1"); // 400
    })(); */
    /* 案例三 */
    /*   (async function () {
      const p1 = Promise.reject("err错误"); //reject 状态
      try {
        p1.then((res) => {
          console.log(res, "res");
        });
      } catch (err) {
        console.error(err, "err"); //try catch 相当于 promise 的 catch
      }
    })(); */
    /* 案例四 */
    /*  (async function () {
      const p1 = Promise.reject("err错误");
      const res = await p1; //await 相当于 then, promise返回的是reject，.then()不会执行，所以下面不会输出且报错
      console.log(res, "res");
    })(); */
    function fn() {
      return 123;
    }
    (async function () {
      let res = await fn(); //
      console.log(res, "res"); //123
    })();
  </script>
</html>
